$(document).ready(function () {
    initPagination();
});

// 领取时间
$('#start-lqdatetime').on('focus',function(){
    WdatePicker({
        //minDate:'%y-%M-{%d+1}' ,
        maxDate:'#F{$dp.$D(\'end-lqdatetime\',{d:0});}',
        onpicked:function(){
            console.log($(this).val());
        }
    })
});
$('#end-lqdatetime').on('focus',function(){
    WdatePicker({
        minDate:'#F{$dp.$D(\'start-lqdatetime\',{d:0});}',
        onpicked:function(){
            console.log($(this).val());
        }

    })
});
//发布时间
$('#start-fbdatetime').on('focus',function(){
    WdatePicker({
        //minDate:'%y-%M-{%d+1}' ,
        maxDate:'#F{$dp.$D(\'end-fbdatetime\',{d:0});}',
        onpicked:function(){
            console.log($(this).val());
        }
    })
});
$('#end-fbdatetime').on('focus',function(){
    WdatePicker({
        minDate:'#F{$dp.$D(\'start-fbdatetime\',{d:0});}',
        onpicked:function(){
            console.log($(this).val());
        }

    })
});


//条件查询开始
$("#distpicker1").distpicker({
    autoSelect: false
});

$("#distpicker2").distpicker({
    autoSelect: false
});


//获取搜索条件
$('.seek-btn').on('click', function () {
    initPagination();
})
//清空条件
$('.seek-clear').on('click', function () {
    $('.form-control.uname').val('');
    $('.form-control.uphone').val('');
    $('#start-time').val('');
    $('#end-time').val('');
    $('#start-lqdatetime').val('');
    $('#end-lqdatetime').val('');
    $('#start-fbdatetime').val('');
    $('#end-fbdatetime').val('');
    $('#casetype').val('');
    $('#level_option').val('');
    $('#buystatus_option').val('');
    $('#gmfs_option').val('');
    $('.province option:eq(0)').prop('selected',true);
    $('.city option:eq(0)').prop('selected',true);
    $('.district option:eq(0)').prop('selected',true);
    $('#province2 option:eq(0)').prop('selected',true);
    $('#city2 option:eq(0)').prop('selected',true);
    $('#district2 option:eq(0)').prop('selected',true);

})


function showfabu(obj){
    var tr = $(obj).parents("tr");
    var id_v=  $(obj)["0"].dataset.value;
    $('#id_edit').val(id_v);
    $('#level_edit').val(tr.find("td.level")["0"].innerText);
    $('#remark_edit').val(tr.find("td.remark")["0"].innerText);
    $("#fabulg").show();

}

function editshow(obj){
    var tr = $(obj).parents("tr");
    var id_v=  $(obj)["0"].dataset.value;
    $('#id_edit2').val(id_v);
    $('#level_edit2').val(tr.find("td.level")["0"].innerText);
    $('#remark_edit2').val(tr.find("td.remark")["0"].innerText);
    $("#editlg").show();
}


function gmfs_change() {
    initPagination()
}
//发布
function fabu() {
    $('#fabulg').cmsmodal('hide');
    var level_v=$("#level_edit").val();
    var remark_v=$("#remark_edit").val();
    var id_v=$("#id_edit").val();
    var json={id:id_v,level:level_v,remark:remark_v};

    $.ajax(
        {
            type: "post",
            url: "/caseinvstg/update",
            data: json,
            dataType: "json",
            async: false,
            success: function (data) {
                result = data;
                alert("发布成功");
            }, error: function () {
            alert("报错了");
        }
        }
    );
    initPagination();

}
//发布
function fabu2() {
    $('#fabulg').cmsmodal('hide');
    var level_v=$("#level_edit2").val();
    var remark_v=$("#remark_edit2").val();
    var id_v=$("#id_edit2").val();
    var json={id:id_v,level:level_v,remark:remark_v};

    $.ajax(
        {
            type: "post",
            url: "/caseinvstg/update",
            data: json,
            dataType: "json",
            async: false,
            success: function (data) {
                result = data;
                hideDialog();
                alert("编辑成功");
            }, error: function () {
            alert("报错了");
        }
        }
    );

    initPagination();

}

function hideDialog() {
    $("#fabulg").hide();
    $("#editlg").hide();
    $("#level_edit").val('');
    $("#remark_edit").val('');
    $("#id_edit").val('');
    $("#level_edit2").val('');
    $("#remark_edit2").val('');
    $("#id_edit2").val('');
}




function  getUphoneArea(){

    var provinceName = $('#province2 option:selected')["0"].value;//省
    var cityName=$('#city2 option:selected')["0"].value;
    var district=$('#district2 option:selected')["0"].value;
    var area_v="";
    if(provinceName){
        area_v= provinceName.replace('省' ,'');
        area_v= area_v.replace('市' ,'');
    }
    if(cityName){
        area_v=area_v+" "+cityName ;
    }
    if(district){
        area_v=area_v+" " +district;
    }
    console.log(area_v);
    return  area_v;
}
function  getUserArea(){

    var provinceName = $('.province option:selected')["0"].value;//省
    var cityName=$('.city option:selected')["0"].value;
    var district=$('.district option:selected')["0"].value;
    var area_v="";
    if(provinceName){
        area_v=provinceName;
    }
    if(cityName){
        area_v=area_v+" "+cityName;
    }
    if(district){
        area_v=area_v+" " +district;
    }
    console.log(area_v);
    return  area_v;
}

function getJson(page_index, jq) {
    if (typeof(page_index) == "undefined") {
        page_index = 0;
    }
    var queryjson = {};
    if ($('.form-control.uname').val()) {
        queryjson.uname = $('.form-control.uname').val();
    }else {
        queryjson.uname='';
    }
    if ($('.form-control.uphone').val()) {
        queryjson.uphone = $('.form-control.uphone').val();
    }else {
        queryjson.uphone='';
    }
    var area_v= getUserArea();
    if (area_v) {
        queryjson.area = area_v;
    }else {
        queryjson.area='';
    }
    var uphonearea_v= getUphoneArea();
    if (uphonearea_v) {
        queryjson.uphonearea = uphonearea_v;
    }else {
        queryjson.uphonearea='';
    }



    if ($('#start-time').val()) {
        queryjson.createTimestart = $('#start-time').val();
    }else{
        queryjson.createTimestart='';
    }
    if ($('#end-time').val()) {
        queryjson.createTimeend = $('#end-time').val();
    }else {
        queryjson.createTimeend='';
    }

    if ($('#start-lqdatetime').val()) {
        queryjson.lqdatetimestart = $('#start-lqdatetime').val();
    }else{
        queryjson.lqdatetimestart='';
    }
    if ($('#end-lqdatetime').val()) {
        queryjson.lqdatetimeend = $('#end-lqdatetime').val();
    }else {
        queryjson.lqdatetimeend='';
    }


    if ($('#start-fbdatetime').val()) {
        queryjson.fbdatetimestart = $('#start-fbdatetime').val();
    }else{
        queryjson.fbdatetimestart='';
    }
    if ($('#end-lqdatetime').val()) {
        queryjson.fbdatetimeend = $('#end-fbdatetime').val();
    }else {
        queryjson.fbdatetimeend='';
    }



    if ($('#casetype').val()) {
        queryjson.casetype = $('#casetype').val();
    }else {
        queryjson.casetype='';
    }
    if ($('#buystatus_option').val()) {
        queryjson.buystatus = $('#buystatus_option').val();
    }else {
        queryjson.buystatus='';
    } if ($('#gmfs_option').val()) {
        queryjson.gmfs = $('#gmfs_option').val();
    }else {
        queryjson.gmfs='';
    }
   if ($('#level_option').val()) {
        queryjson.level = $('#level_option').val();
    }else {
        queryjson.level='';
    }

    queryjson.pageNum = page_index + 1;
    return queryjson;
}


function getCount() {
    var result = 0;
    $.ajax(
        {
            type: "post",
            url: "/caseinvstg/count",
            data: getJson(0, 0),
            dataType: "json",
            async: false,
            success: function (data) {
                result = data;
            }, error: function () {
            alert("报错了");
        }
        }
    );
    return result;
}

//分页
function initPagination() {
    var totalcount = getCount();
    $("#currentCount").text("总条数："+totalcount);
    // 创建分页
    $("#Pagination").pagination(totalcount, {
        num_edge_entries: 0, //边缘页数
        num_display_entries: 8, //主体页数
        callback: pageselectCallback, //回调函数
        items_per_page: 10, //每页显示10项
        prev_text: "上一页",
        next_text: "下一页",
        ellipse_text: "...",//省略的文本
        prev_show_always: true,//是否总显示“前一页”分页按钮
        next_show_always: true,//是否总显示“下一页”分页按钮，
    });




}








 function showDetail(that) {
    if ($('.detail-text-box').length > 0) {
        $('.detail-text-box').remove();
    }else{
        var  txt=that.innerText;
        var str =
            '<div class="detail-text-box">' +
            '<div class="detail-box-close">x</div>' +
            '<div class="detail-text-content"> ' + txt + '</div>' +
            '</div>';
        $(str).appendTo($(that));
        $('.detail-text-box').show();
        if($(that).offset().top - $('.table-box').offset().top < $('.detail-text-box').height()){
            $('.detail-text-box').css({
                top : 0,
            })
        }else{
            $('.detail-text-box').css({
                bottom : 0,
            })
        }

        if($('.detail-text-box').height() > $('.table-box').height()){
            $('.detail-text-box').width(800)
        }
    }

}




function pageselectCallback(page_index, jq) {
    query(page_index, jq);
}
//查询
function query(page_index, jq) {
    $.ajax(
        {
            type: "post",
            url: "/caseinvstg/query",
            data: getJson(page_index, jq),
            dataType: "json",
            success: function (data) {
                var result = data;
                var tmpl1 = bt('tbodyscript', result);
                $("#tbody").html(tmpl1);
            }, error: function () {
            alert("报错了");
        }
        }
    );
}

function exportxls(){
    var json = getJson();
    location.href="/caseinvstg/xls?uname="+json.uname+"&uphone="+json.uphone+"&createTimestart="+json.createTimestart
        +"&createTimeend="+json.createTimeend +"&casetype="+json.casetype+"&fbdatetimestart="+json.fbdatetimestart
        +"&fbdatetimeend="+json.fbdatetimeend +"&lqdatetimestart="+json.lqdatetimestart
        +"&lqdatetimeend="+json.lqdatetimeend +"&level="+json.level+"&buystatus="+json.buystatus  ;
}

